import pygame
import game_object

class Phoenix(game_object.GameObject):
    
    IMAGES = []
    WIDTH = 65
    HEIGHT = 65
    DURATIONS = [0.2, 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1, 0.1, 0.1]
    
    def __init__(self, screen_width, screen_height):
        game_object.GameObject.__init__(self, screen_width, screen_height)
        if not len(Phoenix.IMAGES):
            Phoenix.IMAGES = game_object.slice_sprites(Phoenix.WIDTH, Phoenix.HEIGHT, '../res/images/phoenix.png')
        self.collide_sound = pygame.mixer.Sound("../res/sounds/wall_collision.ogg")
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.state = 0
        self.animation_time = 0.0
        self.frame_duration = Phoenix.DURATIONS[self.state]
        self.image = Phoenix.IMAGES[self.state]
        self.x = 352
        self.y = 500
        self.xVel = 0
        self.yVel = 0
        self.rect = pygame.Rect((self.x, self.y), (Phoenix.WIDTH, Phoenix.HEIGHT))
        

    def update(self, time):
        self.animation_time += time
        if self.animation_time > self.frame_duration:
            self.animation_time = 0.0
            self.state += 1
            if self.state >= len(Phoenix.IMAGES):
                self.state = 0
            self.frame_duration = Phoenix.DURATIONS[self.state]
            self.image = Phoenix.IMAGES[self.state]
        self.move(time * self.xVel, time * self.yVel)

    def set_x_velocity(self, vel):
        self.xVel = vel

    def set_y_velocity(self, vel):
        self.yVel = vel

    def move(self, xDiff, yDiff):
        self.x += xDiff
        self.y += yDiff
        
        if(self.x < 0):
            self.x = 0
            self.collide_sound.play()
        if(self.y < 0):
            self.y = 0
            self.collide_sound.play()
        if(self.x > self.SCREEN_WIDTH - self.WIDTH):
            self.x = self.SCREEN_WIDTH - self.WIDTH
            self.collide_sound.play()
        if(self.y > self.SCREEN_HEIGHT - self.HEIGHT):
            self.y = self.SCREEN_HEIGHT - self.HEIGHT
            self.collide_sound.play()
            
        self.rect = pygame.Rect((self.x, self.y), (Phoenix.WIDTH, Phoenix.HEIGHT))

